<template>
  <div id="adduser">
    <el-dialog   :title="datas.account?'修改用户':'注册用户'" :visible.sync="dialogVisible" top="3vh" width="35%" :close-on-click-modal='false'>
      <div style="width:100%;margin: 0 auto;padding-bottom:20px;" v-loading="loading">
        <div style="margin-top:10px;" class="tabbox">
          <p style=""><a style="color:red;">* </a>姓名</p>
          <el-input  style='width:75%;' placeholder="请输入" v-model="datas.username" clearable></el-input>
        </div>
        <div style="margin-top:10px;" class="tabbox">

            <p style=""><a style="color:red;">* </a>部门</p>
            <el-select  style="width:75%;border:none;" class="pboxc1" v-model="datas.depId" clearable  placeholder="请选择">
              <el-option v-for='item in $store.state.user.bumen' :key='item.id' :label="item.name" :value="item.id"></el-option>
            </el-select>
        </div>
        <div style="margin-top:10px;" class="tabbox">
          <p style=""><a style="color:red;">* </a>权限</p>
          <el-select  style="width:75%;border:none;" class="pboxc1" v-model="datas.roleName" clearable  placeholder="请选择">
            <el-option v-for='item in $store.state.user.role' :key='item.id' :label="item.name" :value="item.id"></el-option>
          </el-select>
        </div>
        <div style="margin-top:10px;" class="tabbox">
          <p style=""><a style="color:red;">* </a>账号</p>
          <el-input  style='width:75%;' placeholder="请输入" v-model="datas.account" clearable></el-input>
        </div>
        <div  style="margin-top:10px;" class="tabbox">
          <p style=""><a style="color:red;">* </a>密码</p>
          <el-input style='width:75%;' placeholder="请输入" v-model="datas.password" type='password' clearable></el-input>
        </div>
        <div  style="margin-top:10px;" class="tabbox">
          <p style=""><a style="color:red;">* </a>确认密码</p>
          <el-input style='width:75%;' placeholder="请输入" v-model="datas.numpassword" type='password' clearable></el-input>
        </div>
        <div style="margin-top:10px;" class="tabbox">
          <p style=""><a style="color:red;">* </a>手机号</p>
          <el-input style='width:75%;' placeholder="请输入" v-model="datas.phoneNum" clearable></el-input>
        </div>
        <div style="margin-top:10px;" class="tabbox">
          <p style=""><a style="color:red;"> </a>邮箱</p>
          <el-input  style='width:75%;' placeholder="请输入" v-model="datas.email" clearable></el-input>
        </div>
      </div>
      <div style="width:100%;height:40px;margin-top:20px;">
        <div style="width:170px;margin: 0 auto;">
          <el-button type="danger" @click="qf()" >取 消</el-button>
          <el-button type="primary" @click="gitact" style='float:right;'>确 定</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>
<script>
  import { register_edit } from '@/api_bast/user'
  export default {
    name: 'adduser',
    computed:{
        indexof(){//查询返回(下标+1)
          return (id,data)=>{
              for(let i in data){
                  if(id==data[i].id){
                    return Number(i)+1;
                  }
              }
          }
        }
    },
    data() {
      return {
        loading:false,
        dialogVisible:false,
      }
    },
    props:{
      datas:Object,
      edit:Number
    },
    methods: {
      qf(){this.dialogVisible = !this.dialogVisible},
      gitact(){//提交数据
        if(this.isNull(this.datas.username)){this.$message({message: '请输入 姓名',type: 'error'});return false}
        if(!this.datas.depId){this.$message({message: '请选择 部门',type: 'error'});return false}
        if(!this.datas.roleName){this.$message({message: '请选择 权限',type: 'error'});return false}
        if(this.isNull(this.datas.account)){this.$message({message: '请输入 账号',type: 'error'});return false}
        if(this.edit==0){
         if(this.isNull(this.datas.password)){this.$message({message: '请输入 密码',type: 'error'});return false}
         if(this.isNull(this.datas.numpassword)){this.$message({message: '请输入 确认密码',type: 'error'});return false}
         if(this.datas.password !== this.datas.numpassword){this.$message({message:'两次输入的密码不相同',type:'error'});return false}
        }
        if(this.isNull(this.datas.phoneNum)){this.$message({message:'请输入 手机号',type: 'error'});return false}
        if(this.isphone(this.datas.phoneNum)){this.$message({message:'手机号码格式不正确',type: 'error'});return false}
        if(!this.isNull(this.datas.email)){if(this.isemail(this.datas.email)){this.$message({message:'邮箱格式不正确',type: 'error'});return false}}
        this.datas.roleId = this.indexof(this.datas.roleName,this.$store.state.user.role);
        this.loading=true;
        register_edit(this.edit,this.datas).then(response => {
          if(response.code==200){
            this.$parent.git_act();
            this.qf();
            this.loading = false;
            this.$notify.success({title: '成功',message: response.message});
          }
        })
      },
    },
    mounted() {
        // this.$parent.qin();//调用父组件方法
    }
  }
</script>

<style scoped>
  .tabbox{
    /*width:100%;*/
    height:40px;
    line-height: 30px;
    margin-top:23px;
  }
  .tabbox p{
    font-size:14px;
    color:#000000;
    width:17%;
    text-align:right;
    margin-right:3%;
    float:left;
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
    font-family:Source Han Sans CN;
  }
</style>
